package com.cloudcc.mobile.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudcc.cloudframe.adapter.CommonAdapter;
import com.cloudcc.cloudframe.adapter.ViewHolder;
import com.cloudcc.cloudframe.util.DateUtils;
import com.cloudcc.mobile.AppConstant;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.schedule.ScheduleEntity;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.util.ViewUtils;
import com.litesuits.android.log.Log;

import java.util.Date;

/**
 * @Description: 日程列表
 * @author wangzhiqiang
 * @date 2015-11-27 上午10:35:53
 * 
 */
public class ScheduleListAdapter extends CommonAdapter<ScheduleEntity> {

	private OnScheduleItemClickListener mOnScheduleItemClickListener;
	private int mTimeLinePos = -1;// 时间线
	private  String mEns = RunTimeManager.getInstance().getlanguage();
	public ScheduleListAdapter(Context mContext) {
		super(mContext);
	}

	@Override
	public void convert(ViewHolder holder, ScheduleEntity t) {

		// 首先 确定是日程 还是任务 istask=1:任务 0: 日程
		boolean isTask = t.isTask();
		final int pos = holder.getPostion();
		final ScheduleEntity tt = t;
		ImageView isFinishView = holder.getView(R.id.type_image);
		TextView tvBeginTime = holder.getView(R.id.time);
		//
		isFinishView.setVisibility(isTask ? View.VISIBLE : View.GONE);
		tvBeginTime.setVisibility(isTask ? View.INVISIBLE : View.VISIBLE);
		ImageView imgScheView = holder.getView(R.id.imgScheView);
		// 前面图标
		if (isTask) {
			imgScheView.setImageResource(R.drawable.scheduleevent_event_task);
			boolean isstatus = TextUtils.equals(t.status, "已结束");
			boolean isstatuss = TextUtils.equals(t.status, "completed");
			if("en".equals(mEns)) {
				isFinishView
						.setBackgroundResource(isstatuss ?R.drawable.task_finished_en
								: R.drawable.toumingde);
//				isFinishView
//						.setBackgroundResource(isstatus ? R.drawable.task_finished_en
//								: R.drawable.toumingde);
			}else{
//				isFinishView
//						.setBackgroundResource(isstatuss ?R.drawable.task_finished
//								: R.drawable.toumingde);
				isFinishView
						.setBackgroundResource(isstatus ? R.drawable.task_finished
								: R.drawable.toumingde);
			}

			holder.getView(R.id.type_image).setOnClickListener(
					new OnClickListener() {
						@Override
						public void onClick(View v) {
							if (mOnScheduleItemClickListener != null) {
								mOnScheduleItemClickListener.OnItemClickHeader(
										pos, tt);
							}
						}
					});
		} else {
			imgScheView.setImageResource(R.drawable.scheduleevent_event_event);
			
			// 第一列 任务 日程 显示 开始时间
			String fieldOne = "";
			Date begintime = t.getBegintimeForDate();
			if (begintime != null) {
				fieldOne = DateUtils.dateToString(begintime,
						AppConstant.DATEPATTERN_TIME);
				Log.d("schedule_time", "shijianshi:" + fieldOne);
			}
			tvBeginTime.setText(fieldOne);
			Log.d("schedule_time", "shijianshi:" + fieldOne);

		}

		// 第二列
		TextView subJectText = holder.getView(R.id.schedule_name);
		TextView subJectTextValue = holder.getView(R.id.schedule_namevalue);
		ViewUtils.setText(subJectText, t.subject);
		if("".equals(t.remark)){
			subJectTextValue.setVisibility(View.GONE);
		}else{
		subJectTextValue.setVisibility(View.VISIBLE);
		ViewUtils.setText(subJectTextValue, t.remark);
		}
//		Log.d("mydata", t.subject.toString());
		// StringBuilder sb = new StringBuilder();
		// String a[] = t.subject.toString().split("");
		// for (int i = 0; i < a.length; i++) {
		// String string = a[i];
		// sb.append(string);
		// sb.append(",");
		// }

		// 第三列 图片
		holder.getConvertView().setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mOnScheduleItemClickListener != null) {
					mOnScheduleItemClickListener.onItemScheduleClick(pos, tt);
				}
			}
		});

		// 时间线
		holder.setVisibility(R.id.schedule_time_area,
				mTimeLinePos == pos ? View.VISIBLE : View.GONE);
		holder.setVisibility(R.id.schedule_listview_line,
				mTimeLinePos != pos ? View.VISIBLE : View.GONE);
		holder.setText(R.id.schedule_time, DateUtils.getNow("HH:mm"));

	}

	public interface OnScheduleItemClickListener {
		/**
		 * 点击编辑 task完成的时候
		 * 
		 * @param pos
		 * @param t
		 */
		void OnItemClickHeader(int pos, ScheduleEntity t);

		/**
		 * 点击单条的时候
		 * 
		 * @param pos
		 * @param t
		 */
		void onItemScheduleClick(int pos, ScheduleEntity t);
	}

	/**
	 * 刷新时间线
	 */
	public void refreshTimeLine() {
		int op = -1;
		Date currentDate = new Date();
		for (int i = 1; i < getCount(); i++) {
			ScheduleEntity preScheduleEntity = getItem(i - 1);
			ScheduleEntity nextScheduleEntity = getItem(i);
			Date preBegintimeForDate = preScheduleEntity.getBegintimeForDate();
			Date nextBegintimeForDate = nextScheduleEntity
					.getBegintimeForDate();

			if (preScheduleEntity.isTask() || preBegintimeForDate == null
					|| nextScheduleEntity.isTask()
					|| nextBegintimeForDate == null) {
				continue;
			}
			// 第一个的开始时间小于当前时间
			// 第二个的开始时间大于等于当前时间
			if (currentDate.after(preBegintimeForDate)
					&& (currentDate.before(nextBegintimeForDate) || currentDate
							.equals(nextBegintimeForDate))) {
				op = i;
				break;
			}
		}
		mTimeLinePos = op;
		notifyDataSetChanged();

	}

	public OnScheduleItemClickListener getOnScheduleItemClickListener() {
		return mOnScheduleItemClickListener;
	}

	public void setOnScheduleItemClickListener(
			OnScheduleItemClickListener mOnScheduleItemClickListener) {
		this.mOnScheduleItemClickListener = mOnScheduleItemClickListener;
	}

	@Override
	public int getLayoutId() {
		return R.layout.schedule_list_item;
	}

}
